<?php
/**
 * +--------------------------------------------------------+
 * @Created by PhpStorm.
 * @Category Permission.php
 * @Depiction:
 * Author: hwy
 * Version: 1.0
 * Modified at: 2019/10/16  14:23
 * +--------------------------------------------------------+
 */

namespace App\Models;

use app\Lib\Org\Data;
use Spatie\Permission\Models\Permission as SpatiePermission;

class Permission extends SpatiePermission
{

    /**
     * 获取全部菜单
     * @param  string $type tree获取树形结构 level获取层级结构
     * @param  string $order 排序字段
     * @return array       	结构数据
     */
    public function getTreeData($type='tree',$order='')
    {
        if (empty($order)) {
            $data=$this
                ->select('id','pid','name','display_name')
                ->get()
                ->toArray();
        }else{
            $data=$this
                ->select('id','pid','name','display_name')
                ->orderBy($order)
                ->get()
                ->toArray();
        }
        // 获取树形或者结构数据
        if($type=='tree'){
            $data=Data::tree($data,'display_name','id','pid');
        }elseif($type="level"){
            $data=Data::channelLevel($data,0,'&nbsp;','id');
        }
        return $data;
    }

}
